#include <stdio.h>
#include <math.h>
#include "app_config.h"
#include "wifi.h"

/*
    1. WiFi 选项 - 1.配网模式 2.状态及信息
*/

static bool select_item = false;

#define ip2str(ip) ((uint8_t *)ip)[0], ((uint8_t *)ip)[1], ((uint8_t *)ip)[2], ((uint8_t *)ip)[3]

static void timer_task_cb(struct _lv_timer_t *timer)
{
    lv_obj_t *list = timer->user_data;
    lv_obj_t *label = lv_obj_get_child(list, 0);
    lv_obj_t *sw = lv_obj_get_child(label, 0);
    if (wifi_stack->smart)
        lv_obj_add_state(sw, LV_STATE_CHECKED);
    else
        lv_obj_clear_state(sw, LV_STATE_CHECKED);
    uint32_t ip = wifi_get_localIP();
    if (ip && !wifi_stack->smart)
    {
        wifi_ap_record_t ap_info = {0};
        esp_wifi_sta_get_ap_info(&ap_info);
        label = lv_obj_get_child(list, 1);
        lv_label_set_text_fmt(label, "地址 : %u.%u.%u.%u", ip2str(&ip));
        ip = wifi_get_subnetMask();
        label = lv_obj_get_child(list, 2);
        lv_label_set_text_fmt(label, "掩码 : %u.%u.%u.%u", ip2str(&ip));
        ip = wifi_get_gateway();
        label = lv_obj_get_child(list, 3);
        lv_label_set_text_fmt(label, "网关 : %u.%u.%u.%u", ip2str(&ip));
        label = lv_obj_get_child(list, 4);
        lv_label_set_text_fmt(label, "信号 : %d", ap_info.rssi);
    }
    else
    {
        label = lv_obj_get_child(list, 1);
        lv_label_set_text_static(label, " ");
        label = lv_obj_get_child(list, 2);
        lv_label_set_text_static(label, " ");
        label = lv_obj_get_child(list, 3);
        lv_label_set_text_static(label, " ");
        label = lv_obj_get_child(list, 4);
        lv_label_set_text_static(label, " ");
    }
}

lv_timer_t *wifi_config_enter(lv_obj_t *parent)
{
    if (!parent)
        return NULL;

    lv_obj_t *list = lv_list_create(parent);
    lv_obj_set_size(list, LV_PCT(100), LV_PCT(100));
    lv_obj_set_style_radius(list, 0, LV_PART_MAIN);
    lv_obj_set_style_border_width(list, 0, LV_PART_MAIN);
    lv_obj_set_style_pad_row(list, 0, LV_PART_MAIN);
    lv_obj_set_flex_flow(list, LV_FLEX_FLOW_COLUMN);
    lv_obj_set_style_bg_color(list, lv_color_make(0, 0, 0), LV_PART_MAIN);
    lv_obj_set_scrollbar_mode(list, LV_SCROLLBAR_MODE_OFF);

    lv_obj_t *label = lv_list_add_text(list, "配网状态");
    lv_obj_set_style_bg_color(label, lv_color_make(64, 64, 64), LV_PART_MAIN);
    lv_obj_set_style_text_color(label, lv_color_make(255, 255, 255), LV_PART_MAIN);
    lv_obj_t *sw = lv_switch_create(label);
    lv_obj_align(sw, LV_ALIGN_RIGHT_MID, -5, 0);

    for (uint8_t i = 0; i < 4; i++)
    {
        lv_obj_t *label = lv_list_add_text(list, " ");
        lv_obj_set_style_bg_color(label, lv_color_make(64, 64, 64), LV_PART_MAIN);
        lv_obj_set_style_text_color(label, lv_color_make(255, 255, 255), LV_PART_MAIN);
    }

    lv_timer_t *timer = lv_timer_create(timer_task_cb, 1000, list);
    return timer;
}

void wifi_config_level(lv_timer_t *timer, void *user_data)
{
    lv_obj_t *obj = timer->user_data;
    lv_timer_del(timer);
    lv_obj_del(obj);
}
uint8_t wifi_config_update_cb(lv_timer_t *timer, void *user_data, uint8_t event)
{
    lv_obj_t *list = timer->user_data;
    if (event == APP_EVENT_TYPE_NEXT)
    {
        lv_obj_t *item = lv_obj_get_child(list, 0);
        select_item = !select_item;
        if (select_item)
        {
            lv_obj_set_style_text_color(item, lv_color_make(255, 165, 2), LV_PART_MAIN);
            // lv_obj_set_style_border_color(item, lv_color_make(255, 165, 2), LV_PART_MAIN);
        }
        else
        {
            // lv_obj_set_style_border_color(item, lv_color_make(64, 64, 64), LV_PART_MAIN);
            lv_obj_set_style_text_color(item, lv_color_make(255, 255, 255), LV_PART_MAIN);
        }
        return 1;
    }
    else if (event == APP_EVENT_TYPE_CLICK)
    {
        if (!select_item)
            return 1;
        lv_obj_t *item = lv_obj_get_child(list, 0);
        lv_obj_t *sw = lv_obj_get_child(item, 0);
        uint8_t enable = 1;
        if (lv_obj_has_state(sw, LV_STATE_CHECKED))
        {
            lv_obj_clear_state(sw, LV_STATE_CHECKED);
            enable = 0;
        }
        else
            lv_obj_add_state(sw, LV_STATE_CHECKED);

        wifi_set_smartconfig(enable, SC_TYPE_ESPTOUCH_AIRKISS);
        return 1;
    }
    else if (event == APP_EVENT_TYPE_CONFIRM) /* 进入 / 退出页面 */
    {
        wifi_config_level(timer, user_data);
        return 0;
    }
    return 0;
}

config_item_s wifi_config_item = {
    .name = "WiFi",
    .enter = wifi_config_enter,
    .level = wifi_config_level,
    .update = wifi_config_update_cb,
    .user_data = NULL,
};
